#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

int n, m;
int f[N], d[N];
void init(int n) { rep(i, 1, n) f[i] = i, d[i] = 0; }
int find(int x) {
  if (f[x] == x) return x;
  int fx = find(f[x]);
  d[x] = (d[x] + d[f[x]]) % 3;
  return f[x] = fx;
}
bool merge(int x, int y, int z) {  // d[x->y] = z
  if (x > n || x < 1 || y > n || y < 1) return false;

  int fx = find(x), fy = find(y);
  if (fx == fy) {
    return (d[x] - d[y] + 3) % 3 == z;
  }
  f[fy] = fx;
  // d[x] = d[x->y] + d[y->fy] + d[fy->fx];
  d[fy] = (d[x] - z - d[y] + 6) % 3;
  return true;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  init(n);
  int cnt = 0;
  while (m--) {
    int x, y, z;
    cin >> z >> x >> y;
    if (!merge(x, y, z - 1)) {
      cnt++;
    }
  }
  cout << cnt;
  return 0;
}